//	---------------------------------------------------------------------------
//	jWebSocket - Abstract WebSocket Client
//	Copyright (c) 2010 jWebSocket.org, Alexander Schulze, Innotrade GmbH
//	---------------------------------------------------------------------------
//	This program is free software; you can redistribute it and/or modify it
//	under the terms of the GNU Lesser General Public License as published by the
//	Free Software Foundation; either version 3 of the License, or (at your
//	option) any later version.
//	This program is distributed in the hope that it will be useful, but WITHOUT
//	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//	FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
//	more details.
//	You should have received a copy of the GNU Lesser General Public License along
//	with this program; if not, see <http://www.gnu.org/licenses/lgpl.html>.
//	---------------------------------------------------------------------------
package com.msstudio.websocket.client;

import java.util.Vector;
import java.util.Enumeration;
import com.msstudio.websocket.client.api.WebSocketClientListener;
import com.msstudio.websocket.client.api.WebSocketPacket;
import com.msstudio.websocket.client.event.WebSocketClientEvent;

/**
 *
 * @author aschulze
 */
public abstract class BaseClient {
    Vector mListeners = new Vector(5, 2);//初始容量为5，增量2
    /*
     * The connection has not yet been established.
     */
    public static final int CONNECTING = 0;
    /*
     * The WebSocket connection is established and communication is possible.
     */
    public static final int OPEN = 1;
    /*
     * The connection is going through the closing handshake.
     */
    public static final int CLOSING = 2;
    /*
     * The connection has been closed or could not be opened.
     */
    public static final int CLOSED = 3;
    /*
     * The maximum amount of bytes per frame
     */
    public static final int MAX_FRAME_SIZE = 16384;
    /*
     * 零长度的byte数组对象创建起来将比任何对象都经济――查看编译后的字节码：生成零长度的byte[]对象只需3条操作码，
     * 而Object lock = new Object()则需要7行操作码。

     */
    final private byte[] lock = new byte[0];  // 特殊的instance变量


    // @Override
    public void addListener(WebSocketClientListener aListener) {
        synchronized(lock) {
            mListeners.addElement(aListener);
        }
    }

    // @Override
    public void removeListener(WebSocketClientListener aListener) {
        synchronized(lock) {
            mListeners.removeElement(aListener);
        }        
    }

    // @Override
    public Vector getListeners() {
        return mListeners;
    }

    // @Override
    public void notifyOpened(WebSocketClientEvent aEvent) {
        synchronized(lock) {
            for(Enumeration enumobj = mListeners.elements(); enumobj.hasMoreElements();) {
                WebSocketClientListener lListener = (WebSocketClientListener) enumobj.nextElement();
                lListener.processOpened(aEvent);
            }      
        }
    }

    // @Override
    public void notifyPacket(WebSocketClientEvent aEvent, WebSocketPacket aPacket) {
        synchronized(lock) {
            for(Enumeration enumobj = mListeners.elements(); enumobj.hasMoreElements();) {
                WebSocketClientListener lListener = (WebSocketClientListener) enumobj.nextElement();
                lListener.processPacket(aEvent, aPacket);
            }      
        }
    }

    // @Override
    public void notifyClosed(WebSocketClientEvent aEvent) {
        synchronized(lock) {
            for(Enumeration enumobj = mListeners.elements(); enumobj.hasMoreElements();) {
                WebSocketClientListener lListener = (WebSocketClientListener) enumobj.nextElement();
                lListener.processClosed(aEvent);
            }      
        }
    }
}
